<?php

class Ajax_BackStoreController extends Model3_Controller
{

    public function init()
    {
        $this->view->setUseTemplate(false);
    }

    public function getPackageUserAction()
    {

        $em = $this->getEntityManager('DefaultDb');
        $clientPackageCatalogAdapter = $em->getRepository('DefaultDb_Entities_ClientPackageCatalog');
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $idClient = $post['id'];
            $packages = $clientPackageCatalogAdapter->findBy(array('user' => $idClient));
//            $this->view->package = $packages;
        }
        $this->view->package = $packages;
    }

    public function addPackageAction()
    {
        $em = $this->getEntityManager('DefaultDb');
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $pack = new DefaultDb_Entities_ClientPackageCatalog();
            $userRepos = $em->getRepository('DefaultDb_Entities_User');
            $packagesAdapter = $em->getRepository('DefaultDb_Entities_ClientPackageCatalog');
            $user = $userRepos->find($post['id']);

            if ($post['peso'] != "" && $post['alto'] != "" && $post['ancho'] != "" && $post['profundidad'] != "" && $post['nombre'] != "")
            {

                $peso = $post['peso'];
                $alto = $post['alto'];
                $ancho = $post['ancho'];
                $profundo = $post['profundidad'];
                $nombre = $post['nombre'];
                $precio = 27+((($profundo * $ancho * $alto)*(1))*(0.00023979));

                $pack->setWeight($peso);
                $pack->setHeight($alto);
                $pack->setWidth($ancho);
                $pack->setDepth($profundo);
                $pack->setPrice($precio);
                $pack->setName($nombre);
                $pack->setUser($user);

                $em->persist($pack);
                $em->flush();
            }
            $packages = $packagesAdapter->findBy(array('user' => $user));
            $this->view->packages = $packages;
        }
    }

    public function deletePackageAction()
    {
        $em = $this->getEntityManager('DefaultDb');

        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $packagesAdapter = $em->getRepository('DefaultDb_Entities_ClientPackageCatalog');
            // $userRespos = $em->getRepository('DefaultDb_Entities_User');

            $idUser = $post['id'];
            $idPack = $post['idPackage'];
            $pack = $packagesAdapter->find($idPack);
            $em->remove($pack);
            $em->flush();

            $packages = $packagesAdapter->findBy(array('user' => $idUser));
            $this->view->packages = $packages;
        }
    }
    
    public function getDataOrderAction()
    {
        
    }
    
    public function searchClientsAction()
    {
        $em = $this->getEntityManager('DefaultDb');
        if($this->getRequest()->isPost())
        {
            $pointRepository = $em->getRepository('DefaultDb_Entities_Point');
            $post = $this->getRequest()->getPost();
            $data = (isset($post['paramString'])) ? $post['paramString']:'';
            $state = (isset($post['state'])) ? $post['state']:'';
            $state = $em->getRepository('DefaultDb_Entities_State')->find($state);
            /* @var $pointRepository DefaultDb_Repositories_PointRepository */
            $points = $pointRepository->getPointByNameOrAddress($data,$state);
            $this->view->points = $points;
        }
    }
    
    public function getPointsByCategoryAction()
    {
        $em = $this->getEntityManager('DefaultDb');
        if($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $category = $em->getRepository('DefaultDb_Entities_Category')->find($post['idCategory']);
            $users = $em->getRepository('DefaultDb_Entities_User')->findByCategory($category);
            $this->view->users = $users;
        }
    }
}
